package com.ky.manage.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ky.manage.R;
import com.ky.manage.utils.BitmapUtils;
import com.ky.manage.utils.ImageLoader.ImageLoaderUtils;
import com.ky.manage.utils.ImageUtil;
import com.luck.picture.lib.entity.LocalMedia;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * 添加图片adapter
 */
public class PicAddAdapter extends BaseQuickAdapter<LocalMedia, BaseViewHolder> {
    private List<LocalMedia> list;

    public PicAddAdapter(int layoutId, List<LocalMedia> list) {
        super(layoutId, list);
        this.list = list;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder , LocalMedia media) {
        String path = media.getPath();
        ImageView picIv = baseViewHolder.getView(R.id.fiv);
        ImageView delIv = baseViewHolder.getView(R.id.iv_del);
        delIv.setVisibility(null == path ? View.GONE : View.VISIBLE);
        delIv.setOnClickListener(view -> {
            int index = baseViewHolder.getAdapterPosition();
            if (index != RecyclerView.NO_POSITION) {
                list.remove(index);
                if (list.size() == 0) {
                    list.add(new LocalMedia());
                }
                notifyItemRemoved(index);
                notifyItemRangeChanged(index, list.size());
            }
        });
        //"image/jpeg"
        String mimeType = media.getMimeType();
        if (media.isCut() && !media.isCompressed()) {
            // 裁剪过
            path = media.getCutPath();
        } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
            // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
            path = media.getCompressPath();
        } else {
            // 原图
            path = media.getPath();
        }
        // 图片
        if (null == path) {
            picIv.setImageResource(R.mipmap.add_pic);
        } else {
            Log.d("lhm", "add pic path = " + path);
            /*Bitmap bitmap = BitmapFactory.decodeFile(path);

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            *//*bitmap = ImageUtil.drawTextToCenter(getContext(), bitmap,
                    simpleDateFormat.format(new Date(System.currentTimeMillis())),
                    14, R.color.white);*//*
            bitmap = ImageUtil.drawTextToCenter(getContext(), bitmap,
                    "测试测试测试",
                    54, R.color.status_bar_bg);
            picIv.setImageBitmap(bitmap);*/
            if(path.startsWith("http")) {
                Glide.with(getContext()).load(path).error(R.mipmap.err_img_load).into(picIv);
            } else {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                String text = "科艺发展\n" + simpleDateFormat.format(new Date(System.currentTimeMillis()));
                Bitmap bitmap = BitmapFactory.decodeFile(path);
                bitmap = ImageUtil.drawTextToCenter(getContext(), bitmap, text, 50, R.color.status_bar_bg);
                BitmapUtils.saveBitmap(path, bitmap, getContext());
                ImageLoaderUtils.loadImage(baseViewHolder.itemView.getContext(), path, picIv);
            }
        }
    }
}
